﻿
namespace TelecoSystems.Web.Services.Variants
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using TelecoSystems.Web.Services.Products;
    using TelecoSystems.Web.Models;

    // Implements application logic using the TelecoSystemsContainer context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    [RequiresAuthentication]
    [EnableClientAccess()]
    public class VariantsDomainService : LinqToEntitiesDomainService<TelecoSystemsContainer>
    {
        #region Target Groups
        public IQueryable<TargetGroup> GetTargetGroups()
        {
            return this.ObjectContext.TargetGroups;
        }
        #endregion

        #region Variants
        public IQueryable<Variant> GetVariants()
        {
            return this.ObjectContext.Variants/*.Include("TargetGroup")*/;            
        }

        public IQueryable<Variant> GetVariantsForProduct(int productId)
        {
            return this.ObjectContext.Variants/*.Include("TargetGroup")*/.Where(v => v.ProductID == productId);
        }
        
        [RequiresRole("Manager")]
        public void InsertVariant(Variant variant)
        {
            variant.DateLastModified = variant.DateCreated = DateTime.Now;
            if (this.ObjectContext.Variants.Count() > 0)
            {
                variant.Id = this.ObjectContext.Variants.OrderByDescending(v => v.Id).First().Id + 1;
            }
            else
            {
                variant.Id = 1;
            }

            if ((variant.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(variant, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Variants.AddObject(variant);
            }
        }

        [RequiresRole("Manager")]
        public void UpdateVariant(Variant currentVariant)
        {
            try {
                if (currentVariant.Id == 0) InsertVariant(currentVariant);
                currentVariant.DateLastModified = DateTime.Now;
                this.ObjectContext.Variants.AttachAsModified(currentVariant, this.ChangeSet.GetOriginal(currentVariant));
            } catch (Exception e) {
                //log error
            }
        }

        [RequiresRole("Manager")]
        public void DeleteVariant(Variant variant)
        {
            if ((variant.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Variants.Attach(variant);
            }
            this.ObjectContext.Variants.DeleteObject(variant);
        }
        #endregion
    }
}


